package com.benjaminwan.read.free.config

import com.benjaminwan.read.free.config.Options.getAction
import com.benjaminwan.read.free.config.Options.optionHorizontal
import com.benjaminwan.read.free.config.Options.optionVertical
import com.benjaminwan.read.free.model.ItemConfig
import com.benjaminwan.read.free.model.enums.ItemType
import com.benjaminwan.read.free.model.action.MouseDragPage
import com.benjaminwan.read.free.model.action.Position
import com.benjaminwan.read.free.model.enums.Horizontal
import com.benjaminwan.read.free.model.enums.Vertical
import com.benjaminwan.read.free.model.listOfItemConfig
import com.benjaminwan.read.free.ui.main.MainViewModel
import com.benjaminwan.read.free.utils.*
import com.xuexiang.xui.widget.textview.supertextview.SuperTextView

object MouseDragPageConfig {
    fun getMouseDragPageConfig(mainVM: MainViewModel, pos: Position): List<ItemConfig> {
        return listOfItemConfig {
            itemConfig {
                type = ItemType.Text
                titleStr = "x距离"
                rightTextFunc = { "${getAction<MouseDragPage>(mainVM, pos)?.dxRange}" }
                click = { view ->
                    (view as SuperTextView).showRightTextInputDialog(
                        title, 0, -1, getRegexFilter(numRegex, "[.]+"),
                        { dialog, which ->
                            val default = ""
                            dialog.inputEditText?.setText(default)
                            dialog.inputEditText?.setSelection(default.length)
                        }) { dialog, which ->
                        val input = dialog.inputEditText?.text.toString()
                        val action =
                            getAction<MouseDragPage>(mainVM, pos) ?: return@showRightTextInputDialog
                        val yStart = action.dyRange.first
                        val yEnd = action.dyRange.last
                        if (input.isEmpty() && yEnd != 0) {
                            mainVM.setAction(
                                pos, action.copy(dxRange = (0..0), stepXRange = (1..1))
                            )
                        } else if (input.isEmpty() && yEnd == 0) {
                            mainVM.setAction(
                                pos, action.copy(dxRange = (150..150), stepXRange = (15..15))
                            )
                        } else if (!input.contains("..")) {
                            dialog.view.shakeReverse()
                            view.context.showXToastError("输入格式错误")
                            return@showRightTextInputDialog
                        } else {
                            val range = input.split("..")
                            if (range.isEmpty() || range.size != 2) {
                                dialog.view.shakeReverse()
                                view.context.showXToastError("输入格式错误")
                                return@showRightTextInputDialog
                            }
                            val start = range[0].toIntOrNull()
                            val end = range[1].toIntOrNull()
                            if (start == null || end == null) {
                                dialog.view.shakeReverse()
                                view.context.showXToastError("输入格式错误:数字转换错误")
                                return@showRightTextInputDialog
                            }
                            if (start > end) {
                                dialog.view.shakeReverse()
                                view.context.showXToastError("输入格式错误,起始($start)>结束($end)")
                                return@showRightTextInputDialog
                            }
                            if (start == 0 && end == 0 && yStart == 0 && yEnd == 0) {
                                dialog.view.shakeReverse()
                                view.context.showXToastError("当y距离为($yStart..$yEnd)时，不允许x距离($start..$end)")
                                return@showRightTextInputDialog
                            }
                            mainVM.setAction(pos, action.copy(dxRange = (start..end)))
                        }
                        dialog.dismiss()
                    }
                }
            }
            itemConfig {
                type = ItemType.Text
                titleStr = "y距离"
                rightTextFunc = { "${getAction<MouseDragPage>(mainVM, pos)?.dyRange}" }
                click = { view ->
                    (view as SuperTextView).showRightTextInputDialog(
                        title, 0, -1, getRegexFilter(numRegex, "[.]+"),
                        { dialog, which ->
                            val default = ""
                            dialog.inputEditText?.setText(default)
                            dialog.inputEditText?.setSelection(default.length)
                        }) { dialog, which ->
                        val input = dialog.inputEditText?.text.toString()
                        val action =
                            getAction<MouseDragPage>(mainVM, pos) ?: return@showRightTextInputDialog
                        val xStart = action.dxRange.first
                        val xEnd = action.dxRange.last
                        if (input.isEmpty() && xEnd != 0) {
                            mainVM.setAction(pos, action.copy(dyRange = (0..0),stepYRange = (1..1)))
                        } else if (input.isEmpty() && xEnd == 0) {
                            mainVM.setAction(pos, action.copy(dyRange = (150..150),stepYRange = (15..15)))
                        } else if (!input.contains("..")) {
                            dialog.view.shakeReverse()
                            view.context.showXToastError("输入格式错误")
                            return@showRightTextInputDialog
                        } else {
                            val range = input.split("..")
                            if (range.isEmpty() || range.size != 2) {
                                dialog.view.shakeReverse()
                                view.context.showXToastError("输入格式错误")
                                return@showRightTextInputDialog
                            }
                            val start = range[0].toIntOrNull()
                            val end = range[1].toIntOrNull()
                            if (start == null || end == null) {
                                dialog.view.shakeReverse()
                                view.context.showXToastError("输入格式错误:数字转换错误")
                                return@showRightTextInputDialog
                            }
                            if (start > end) {
                                dialog.view.shakeReverse()
                                view.context.showXToastError("输入格式错误,起始($start)>结束($end)")
                                return@showRightTextInputDialog
                            }
                            if (start == 0 && end == 0 && xStart == 0 && xEnd == 0) {
                                dialog.view.shakeReverse()
                                view.context.showXToastError("当x距离为($xStart..$xEnd)时，不允许y距离($start..$end)")
                                return@showRightTextInputDialog
                            }
                            mainVM.setAction(pos, action.copy(dyRange = (start..end)))
                        }
                        dialog.dismiss()
                    }
                }
            }
            itemConfig {
                type = ItemType.Text
                titleStr = "x方向"
                optionFunc = { optionHorizontal }
                rightTextFunc = { "${getAction<MouseDragPage>(mainVM, pos)?.xDirection}" }
                click = { view ->
                    (view as SuperTextView).showPicker(title, options) { opValue, opName ->
                        if (opName == view.rightString) return@showPicker
                        val action = getAction<MouseDragPage>(mainVM, pos) ?: return@showPicker
                        val direction = Horizontal.valueOf(opName)
                        mainVM.setAction(pos, action.copy(xDirection = direction))
                    }
                }
            }
            itemConfig {
                type = ItemType.Text
                titleStr = "y方向"
                optionFunc = { optionVertical }
                rightTextFunc = { "${getAction<MouseDragPage>(mainVM, pos)?.yDirection}" }
                click = { view ->
                    (view as SuperTextView).showPicker(title, options) { opValue, opName ->
                        if (opName == view.rightString) return@showPicker
                        val action = getAction<MouseDragPage>(mainVM, pos) ?: return@showPicker
                        val direction = Vertical.valueOf(opName)
                        mainVM.setAction(pos, action.copy(yDirection = direction))
                    }
                }
            }
            itemConfig {
                type = ItemType.Text
                titleStr = "x步进"
                rightTextFunc = { "${getAction<MouseDragPage>(mainVM, pos)?.stepXRange ?: ""}" }
                click = { view ->
                    (view as SuperTextView).showRightTextInputDialog(
                        title, 0, -1, getRegexFilter(numRegex, "[.]+"),
                        { dialog, which ->
                            val default = ""
                            dialog.inputEditText?.setText(default)
                            dialog.inputEditText?.setSelection(default.length)
                        }) { dialog, which ->
                        val input = dialog.inputEditText?.text.toString()
                        val action =
                            getAction<MouseDragPage>(mainVM, pos) ?: return@showRightTextInputDialog
                        if (input.isEmpty()) {
                            mainVM.setAction(pos, action.copy(stepXRange = null))
                        } else if (!input.contains("..")) {
                            dialog.view.shakeReverse()
                            view.context.showXToastError("输入格式错误")
                            return@showRightTextInputDialog
                        } else {
                            val range = input.split("..")
                            if (range.isEmpty() || range.size != 2) {
                                dialog.view.shakeReverse()
                                view.context.showXToastError("输入格式错误")
                                return@showRightTextInputDialog
                            }
                            val start = range[0].toIntOrNull()
                            val end = range[1].toIntOrNull()
                            if (start == null || end == null) {
                                dialog.view.shakeReverse()
                                view.context.showXToastError("输入格式错误:数字转换错误")
                                return@showRightTextInputDialog
                            }
                            if (start > end) {
                                dialog.view.shakeReverse()
                                view.context.showXToastError("输入格式错误,起始($start)>结束($end)")
                                return@showRightTextInputDialog
                            }
                            if (start == 0 && end == 0) {
                                dialog.view.shakeReverse()
                                view.context.showXToastError("输入格式错误,不允许起始($start)=0和结束($end)=0")
                                return@showRightTextInputDialog
                            }
                            mainVM.setAction(pos, action.copy(stepXRange = (start..end)))
                        }
                        dialog.dismiss()
                    }
                }
            }
            itemConfig {
                type = ItemType.Text
                titleStr = "y步进"
                rightTextFunc = { "${getAction<MouseDragPage>(mainVM, pos)?.stepYRange ?: ""}" }
                click = { view ->
                    (view as SuperTextView).showRightTextInputDialog(
                        title, 0, -1, getRegexFilter(numRegex, "[.]+"),
                        { dialog, which ->
                            val default = ""
                            dialog.inputEditText?.setText(default)
                            dialog.inputEditText?.setSelection(default.length)
                        }) { dialog, which ->
                        val input = dialog.inputEditText?.text.toString()
                        val action =
                            getAction<MouseDragPage>(mainVM, pos) ?: return@showRightTextInputDialog
                        if (input.isEmpty()) {
                            mainVM.setAction(pos, action.copy(stepYRange = null))
                        } else if (!input.contains("..")) {
                            dialog.view.shakeReverse()
                            view.context.showXToastError("输入格式错误")
                            return@showRightTextInputDialog
                        } else {
                            val range = input.split("..")
                            if (range.isEmpty() || range.size != 2) {
                                dialog.view.shakeReverse()
                                view.context.showXToastError("输入格式错误")
                                return@showRightTextInputDialog
                            }
                            val start = range[0].toIntOrNull()
                            val end = range[1].toIntOrNull()
                            if (start == null || end == null) {
                                dialog.view.shakeReverse()
                                view.context.showXToastError("输入格式错误:数字转换错误")
                                return@showRightTextInputDialog
                            }
                            if (start > end) {
                                dialog.view.shakeReverse()
                                view.context.showXToastError("输入格式错误,起始($start)>结束($end)")
                                return@showRightTextInputDialog
                            }
                            if (start == 0 && end == 0) {
                                dialog.view.shakeReverse()
                                view.context.showXToastError("输入格式错误,不允许起始($start)=0和结束($end)=0")
                                return@showRightTextInputDialog
                            }
                            mainVM.setAction(pos, action.copy(stepYRange = (start..end)))
                        }
                        dialog.dismiss()
                    }
                }
            }
        }
    }
}